package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gexin.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.base.orm.MPage;
import com.weixing.mall.base.orm.PageUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.dto.SaleAgentDto;
import com.weixing.mall.provider.api.model.query.AgentInfoQuery;
import com.weixing.mall.provider.api.model.vo.*;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentBonusGroup;
import com.weixing.mall.provider.service.IAgentBonusGroupService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.service.IUpgradeMsgService;
import com.weixing.mall.util.regex.RegexUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理RPC接口
 *
 * @author  kfc
 * @date Create in 2019/9/18 15:41
 * @since 1.0
 */
@RestController
@RequestMapping("/api/agent")
public class AgentRpcController extends BaseController {
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentBonusGroupService agentBonusGroupService;
    @Autowired
    private IUpgradeMsgService upgradeMsgService;


    @GetMapping("findByMobile")
    public Result<AgentVo> findByMobile(String mobile) {
        AgentVo agent = agentService.selectByMobile(mobile);
        return ResultUtil.success(agent);
    }


    @GetMapping("selectByMobileOrSn")
    public Result<AgentVo> selectByMobileOrSn(String searchParam) {
        AgentVo agentVo = null;
        if (RegexUtil.isMobile(searchParam)) {
            agentVo = agentService.selectByMobile(searchParam);
        } else {
            agentVo = agentService.selectByAgentSn(searchParam);
        }
        return ResultUtil.success(agentVo);
    }

    @GetMapping("selectByMobile")
    public Result<AgentAuthVo> selectByMobile(String mobile) {
        Agent agent = agentService.findByMobile(mobile);
        AgentAuthVo agentAuthVo = new AgentAuthVo();
        BeanUtils.copyProperties(agent, agentAuthVo);
        return ResultUtil.success(agentAuthVo);
    }

    @GetMapping("findByAgentSn")
    public Result<AgentVo> findByAgentSn(String agentSn) {
        AgentVo agentVo = agentService.selectByAgentSn(agentSn);
        return ResultUtil.success(agentVo);
    }

    @GetMapping("findByAgentInfo")
    public Result<AgentVo> findByAgentInfo(AgentInfoQuery agentInfoQuery) {
        AgentVo agentVo = agentService.selectByAgentInfo(agentInfoQuery);
        return ResultUtil.success(agentVo);
    }

    @GetMapping("selectByIdAgentCustomer")
    public Result<AgentCustomerVo> selectByIdAgentCustomer(Long agentId) {
        AgentCustomerVo agentCustomerVo = agentService.selectByIdAgentCustomer(agentId);
        return ResultUtil.success(agentCustomerVo);
    }

    @GetMapping("selectAgentTreeByParentId")
    public Long[] selectAgentTreeByParentId(Long agentId) {
        return agentService.selectAgentTreeByParentId(agentId);
    }

    @GetMapping("selectByParentId")
    public List<AgentVo> selectByParentId(Long agentId) {
        return agentService.selectByParentId(agentId);
    }

    @GetMapping("selectById")
    public Result<AgentVo> selectById(Long agentId) {
        return ResultUtil.success(agentService.selectById(agentId));
    }

    @GetMapping("selectByAgentId")
    public Result selectByAgentId(Long agentId) {
        Agent agent = agentService.getById(agentId);
        if(null == agent){
            return  ResultUtil.error("代理不存在");
        }
        AgentRpcVo vo = new AgentRpcVo();
        if (agent != null) {
            BeanUtils.copyProperties(agent, vo);
        }
        // 查询结构树

        List<AgentRpcVo> treeList = getTreeList(agent);
        vo.setTreeList(treeList);
        // 查询分红关系
        AgentBonusGroup agentBonusGroup = agentBonusGroupService.findByAgentId(agentId);
        if (null != agentBonusGroup) {
            AgentBonusVo agentBonusVo = new AgentBonusVo();
            BeanUtils.copyProperties(agentBonusGroup, agentBonusVo);
            vo.setBonusVo(agentBonusVo);
        }
        // 查询分红关系
        AgentBonusGroup unnormalBonusGroup = agentBonusGroupService.findByAgentIdAndNormalState(agentId, IsEnableEnum.DISABLE.getValue());
        if (null != unnormalBonusGroup) {
            AgentBonusVo agentBonusVo = new AgentBonusVo();
            BeanUtils.copyProperties(unnormalBonusGroup, agentBonusVo);
            vo.setUnnormalBonusVo(agentBonusVo);
        }
        return ResultUtil.success(vo);
    }

    @GetMapping("selectByAgentSn")
    public Result selectByAgentSn(String agentSn) {
        Agent agent = agentService.findByAgentSn(agentSn);
        if (null == agent){
            return  ResultUtil.error("代理不存在");
        }
        AgentRpcVo vo = new AgentRpcVo();
        if (agent != null) {
            BeanUtils.copyProperties(agent, vo);
        }
        // 查询结构树
        List<AgentRpcVo> treeList = getTreeList(agent);
        vo.setTreeList(treeList);
        // 查询分红关系
        AgentBonusGroup agentBonusGroup = agentBonusGroupService.findByAgentId(agent.getId());
        if (null != agentBonusGroup ) {
            AgentBonusVo agentBonusVo = new AgentBonusVo();
            BeanUtils.copyProperties(agentBonusGroup, agentBonusVo);
            vo.setBonusVo(agentBonusVo);
        }
        return ResultUtil.success(vo);
    }

    @GetMapping("selectByAgentMobile")
    public Result selectByAgentMobile(String mobile) {
        Agent agent = agentService.findByMobile(mobile);
        if (null == agent){
            return  ResultUtil.error("代理不存在");
        }
        AgentRpcVo vo = new AgentRpcVo();
        if (agent != null) {
            BeanUtils.copyProperties(agent, vo);
        }
        // 查询结构树
        List<AgentRpcVo> treeList = getTreeList(agent);
        vo.setTreeList(treeList);
        // 查询分红关系
        AgentBonusGroup agentBonusGroup = agentBonusGroupService.findByAgentId(agent.getId());
        if (null != agentBonusGroup ) {
            AgentBonusVo agentBonusVo = new AgentBonusVo();
            BeanUtils.copyProperties(agentBonusGroup, agentBonusVo);
            vo.setBonusVo(agentBonusVo);
        }
        return ResultUtil.success(vo);
    }


    private List<AgentRpcVo> getTreeList(Agent agent) {
//        System.out.println("agent: " + JSON.toJSONString(agent));
        List<AgentRpcVo> list = Lists.newArrayList();
        List<Long> ids = Lists.newArrayList();
        if (0 != agent.getDealerId().longValue()) {
            ids.add(agent.getDealerId());
        }
        if (0 != agent.getLevel1st().longValue()) {
            ids.add(agent.getLevel1st());
        }
        if (0 != agent.getLevel2nd().longValue()) {
            ids.add(agent.getLevel2nd());
        }
        if (0 != agent.getLevel3rd().longValue()) {
            ids.add(agent.getLevel3rd());
        }
        if (0 != agent.getLevel4th().longValue()) {
            ids.add(agent.getLevel4th());
        }
        if (CollUtil.isNotEmpty(ids)) {
            QueryWrapper<Agent> wrapper = new QueryWrapper<Agent>().in("id", ids).orderByDesc("agent_level");
            List<Agent> agents = agentService.list(wrapper);
            AgentRpcVo agentRpcVo = null;
            for (Agent a : agents) {
                agentRpcVo = new AgentRpcVo();
                BeanUtils.copyProperties(a, agentRpcVo);
                list.add(agentRpcVo);
            }
        }

        return list;
    }


    @GetMapping("selectByCustomerIdAgentLevel")
    public List<Long> selectByCustomerIdAgentLevel(int parseInt, long currentUserId) {
        return agentService.selectByCustomerIdAgentLevel(parseInt, currentUserId);
    }

    @GetMapping("selectAgentIdByAgentLevel")
    public List<Long> selectAgentIdByAgentLevel(int agentLevel) {
        return agentService.selectAgentIdByAgentLevel(agentLevel);
    }

    @GetMapping("selectByCustomerId")
    public List<AgentCustomerVo> selectByCustomerId(Long customerId) {
        return agentService.selectByCustomerId(customerId);
    }

    @GetMapping("selectAuthVoByLoginName")
    public Result<AgentAuthVo> selectAuthVoByLoginName(String loginName) {
        Agent byMobile = agentService.findByMobile(loginName);
        AgentAuthVo authVo = null;
        if (null != byMobile) {
            authVo = new AgentAuthVo();
            BeanUtils.copyProperties(byMobile, authVo);
        }
        return ResultUtil.success(authVo);
    }


    @GetMapping("selectChildByAgentId")
    public Result<MPage<AgentRpcVo>> selectChildByAgentId(long currentPage, long pageSize, long agentId) {
        Agent byId = agentService.getById(agentId);
        IPage<AgentRpcVo> page = agentService.selectChildByAgentId(initMpPage(), agentId, byId.getAgentLevel());
        return ResultUtil.success(PageUtil.newPage(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords()));
    }

    @PostMapping("selectChildByAgent")
    public Result<MPage<AgentRpcVo>> selectChildByAgent(@RequestBody SaleAgentDto dto) {
        Integer agentLevel = null;
        if (null != dto.getAgentId()){
            Agent byId = agentService.getById(dto.getAgentId());
            agentLevel = byId.getAgentLevel();
        }
        IPage<AgentRpcVo> page = agentService.selectChildByAgent(initMpPage(), dto, agentLevel);
        return ResultUtil.success(PageUtil.newPage(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords()));
    }


    @GetMapping("selectChildByKefuId")
    public Result<MPage<AgentRpcVo>> selectChildByKefuId(long currentPage, long pageSize, long kefuId) {
        IPage<AgentRpcVo> page = agentService.selectAgentVoByCustomerId(initMpPage(), kefuId);
        return ResultUtil.success(PageUtil.newPage(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords()));
    }

    @PostMapping("updateMobile")
    public Result updateMobile(Long agentId,String mobile) {

        Agent byMobile = agentService.getById(agentId);
        if (null != byMobile) {
            byMobile.setMobile(mobile);
            agentService.updateById(byMobile);
        }
        return ResultUtil.success();
    }

    @PostMapping("updateTradeDate")
    public Result updateTradeDate(Long agentId, String today) {

        Agent byMobile = agentService.getById(agentId);
        if (null != byMobile) {
            byMobile.setTradeDate(today);
            agentService.updateById(byMobile);
        }
        return ResultUtil.success();
    }

    @GetMapping("upgradeMsgTask")
    public Result upgradeMsgTask(){
        upgradeMsgService.upgradeMsgTask();
        return ResultUtil.success();
    }
    @GetMapping("expireTask")
    public Result expireTask(){
        agentService.expireTask();
        return ResultUtil.success();
    }
}
